package orfeu.engine;

import java.util.Vector;

import AI.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class GLView extends GLSurfaceView {
	// private OpenGL renderer member
	private static GLRenderer renderer = new GLRenderer();
	// reference to parent context
	private Context context;
	// private game updater thread runs separately from renderer
	private static GameUpdater updater = new GameUpdater();
	
	private static Hell hell = new Hell();
	
	private HealthBar healthBar = new HealthBar(5, 180, 1, 100, R.drawable.healthbar);
	private RageBar rageBar = new RageBar(5, 180, 1, 100, R.drawable.ragebar);
	public static Sprite gameOver ;
	public static Sprite sheIsLostForver;
	
	// declare game objects here
	Demon s;
	
	public static void reset()
	{
		renderer = new GLRenderer();
		renderer.drawList = new Vector<Sprite>();
		updater = new GameUpdater();
		hell = new Hell();
	}
	
	public static Hell getGenerator()
	{
		return hell;
	}
	
	public static GLRenderer getRenderer()
	{
		return renderer;
	}
	
	public static GameUpdater getUpdater()
	{
		return updater;
	}
	
	//TouchScreen and Accelerometer options for movement
	private SensorManager _sm;
	private Sensor _sa;
	Hero h;
	private static float FRICTION = 0;
	public static boolean canEnterHigh;
	private boolean first_time;
	private float last_sum = 1;
	public boolean landscape;
	private long _last_time;
	private long _interval = 10;
	
	private OnTouchListener selecttouch = new View.OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			float x = event.getX();
			float y = event.getY();
			//Log.d("i","x= " + x +" y= " + y + "limite buton: "+ (Constants.limitRight - 10));
			//Log.d("i","Constants:" + Constants.screenHeight);
			if(x <= Constants.screenWidth &&  x >= Constants.screenWidth -  60
					&& y <= Constants.screenHeight && y >= Constants.screenHeight - 40)
			{
				//Here we put the intent towards the select power activity
				//Log.d("i","S-a apasat pe buton");
				//Intent myIntent = new Intent(GLView.this,PowerActivity.class);
				
			}
			
			//foloseste rage
			if (Hero.getHero().getRage() == Hero.getHero().MAX_HEALTH) {
				for (int i = (int)Constants.limitLeft - 30; i < (int)Constants.limitRight + 50; i = i + 32) {
					Hero.getHero().createHeroProjectile(i, Hero.getHero().yPoz - 40);
				}
				Hero.getHero().setRage(0);
			}
			return false;
		}
		
	};
	
	private final SensorEventListener _sacc = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor arg0, int arg1) {
		}

		@Override
		public void onSensorChanged(SensorEvent arg0) {   
	    	 float x,y;
	    	 //Daca e pe telefon sa foloseasca sistemul de coordonate pe Oy
	    	 if(!landscape){
		    	 x = arg0.values[1];
		    	 y = arg0.values[0];
		    	 //Log.d("i", "x = " + y + "y = " + x);
		    	 
		    	 // subtract g factor
		    	 float gx = 9.81f;
		    	 gx = 0.8f * gx + 0.2f * x;
		    	 x -= gx;
		    	 float auxx = x;
		    	 float auxy = y;
		    	 // normalize
		    	 x = (float) (x / Math.sqrt(auxx * auxx + auxy * auxy));
		    	 y = (float) (y / Math.sqrt(auxx * auxx + auxy * auxy));

		    	
		    	 h.setxPoz(h.getxPoz() - x * 10);	
	    	}
	    	 else{
		    	 //Daca nu sa ramana pe sistemul de coordonate al tabletei, pe Ox
		    	 x = arg0.values[0];
		    	 y = arg0.values[1];
		    	 
		    	 // subtract g factor
		    	 float gx = 9.81f;
		    	 gx = 0.8f * gx + 0.2f * x;
		    	 x -= gx;
		    	 
		    	 float auxx = x;
		    	 float auxy = y;
		    	 // normalize
		    	 x = (float) (x / Math.sqrt(auxx * auxx + auxy * auxy));
		    	 y = (float) (y / Math.sqrt(auxx * auxx + auxy * auxy));

		    	 //Log.d("i", "x = " + x);
		    	 h.setxPoz(h.getxPoz() + x * 10);
	    	 }
		}
    };
	
	/** constructor **/
	public GLView(Context context, Activity activity) {
		super(context);
		this.context = context;
		
		// create the renderer
		//renderer = new GLRenderer();
		renderer.setContext(this.context);
		// create the updater
		//updater = new GameUpdater();
		
		//Create and register the sensor manager
		_sm = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
		_sa = _sm.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
		_sm.registerListener(_sacc,_sa, SensorManager.SENSOR_DELAY_GAME);
		setOnTouchListener(selecttouch);
		//Variabila pentru a calibra accelerometrul
		first_time = true;
		
		// initialize game objects		
		
		
		h = Hero.getHero();
		//Create the background
		Background Bg = new Background(1024,1024,4,100,R.drawable.fundal4);
		
		//Create the select power button and put it somewhere
		Buton selectpower = new Buton(128,64,1,100,R.drawable.selectpower2);
		
		//Create the first projectile
		Projectile proiectil = new Projectile(32,32,1,100,R.drawable.nota2);
		proiectil.setPosition(h.xPoz, h.yPoz - 40);
		proiectil.setDamage(h.damage);
		proiectil.setSpeed(-3*Hell.SPEED);
		h.firstProjectile = proiectil;

		//Create the first demon projectile
		Projectile dummyproj = new Projectile(32,32,1,100,R.drawable.fireball);
		proiectil.setPosition(1000, 0);
		proiectil.setDamage(10000);
		proiectil.setSpeed(3*Hell.SPEED);
		Hell.firstProjectile = dummyproj;
		
		//Create some dummy demons in order to get their Id's, add them to the drawList, but not to the update List
		Sprite dummyFlyer = new Sprite(128, 128, 4, 100, R.drawable.flyer);
		Sprite dummyExploder = new Sprite(128, 128, 4, 100, R.drawable.exploder);
		Sprite dummyCrawler = new Sprite(128, 128, 4, 100, R.drawable.crawler);
		Sprite dummyExplode = new Sprite(128,128, 4,100,R.drawable.explode);
		
		Hell.dummyFlyer = dummyFlyer;
		Hell.dummyCrawler = dummyCrawler;
		Hell.dummyExploder = dummyExploder;
		Hell.dummyExplode = dummyExplode;
		
		//II pozitionam in afara ecranului
		dummyFlyer.px = -400;
		dummyFlyer.py = -400;
		dummyExploder.px = -400;
		dummyExploder.py = -400;
		dummyCrawler.px = -400;
		dummyCrawler.py = -400;
		dummyExplode.px = -400;
		dummyExplode.py = -400;
		
		
		//Initialize the game over messages
		gameOver = new Sprite(512,64,1,100,R.drawable.gameover);
		gameOver.px = -400;
		gameOver.py = -400;
		
		sheIsLostForver = new Sprite(512,64,1,100,R.drawable.sheislostforever);
		sheIsLostForver.px = -400;
		sheIsLostForver.py = -400;
		
		// initialize some game objects in order to get the textureId's
		//The textures are loaded in the onSurfaceCreated method of the renderer so we need to get heir Id's
		
		renderer.drawList.add(proiectil);
		//renderer.drawList.add(selectpower);
		renderer.drawList.add(dummyFlyer);
		renderer.drawList.add(dummyExploder);
		renderer.drawList.add(dummyCrawler);
		renderer.drawList.add(dummyExplode);
		renderer.drawList.add(dummyproj);
		renderer.drawList.add(Bg);
		renderer.drawList.add(gameOver);
		renderer.drawList.add(sheIsLostForver);
		
		
		updater.updateList.add(proiectil);
		updater.updateList.add(dummyproj);
		
		
		
		// add game objects to draw list and update list
//		for (Demon d: Hell.demons) {
//			renderer.drawList.add(d);
//			updater.updateList.add(d);
//	
//		}
		
		renderer.drawList.add(h);
		updater.updateList.add(h);
		renderer.drawList.add(healthBar);
		updater.updateList.add(healthBar);

		renderer.drawList.add(rageBar);
		updater.updateList.add(rageBar);

		setRenderer(renderer);
		Thread t = new Thread(updater);
		t.start();
		Thread h = new Thread(hell);
		h.start();
	}
	
}
